package com.fudan.backend.service.impl;

import com.fudan.backend.dao.CartDao;
import com.fudan.backend.dao.CommodityDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.CartItem;
import com.fudan.backend.pojo.Commodity;
import com.fudan.backend.service.CartService;
import com.fudan.backend.util.Snowflake;
import com.fudan.backend.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CartServceImpl implements CartService {
    @Autowired
    CartDao cartDao;
    @Autowired
    CommodityDao commodityDao;
    @Autowired
    Snowflake snowflake;

    @Autowired
    StringUtil stringUtil;
    @Override
    public List<List<CartItem>> getCartItems(String userId) {
        List<CartItem> cartItems = cartDao.selectCartItemByUserId(userId);
        List<List<CartItem>> cartGroupList = new ArrayList<>();
        cartItems.stream().collect(Collectors.groupingBy(CartItem::getStoreId, Collectors.toList()))
                .forEach((model, list)-> cartGroupList.add(list));
        return cartGroupList;
    }

    @Override
    public List<List<CartItem>> getCartItemsListByCartId(List<String> cartItemIdList) {
        List<List<CartItem>> cartGroupList = new ArrayList<>();
       this.getCartItemsMapByCartId(cartItemIdList)
               .forEach((model, list)-> cartGroupList.add(list));
        return cartGroupList;
    }

    @Override
    public Map<String, List<CartItem>> getCartItemsMapByCartId(List<String> cartItemIdList) {
        if(!stringUtil.checkIdList(cartItemIdList)){
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        List<CartItem> cartItems = cartDao.selectCartItemsByCartItemIdList(cartItemIdList);
        return cartItems.stream().collect(Collectors.groupingBy(CartItem::getStoreId, Collectors.toList()));
    }

    @Override
    public RetCode addCart(String userId, String commodityId, String storeId, Integer amount) {
        if(amount <= 0){
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        Commodity commodity = commodityDao.selectCommodityByCommodityId(commodityId);
        if(commodity == null){
            throw new BaseException(RetCode.COMMODITY_NOT_FOUND);
        }
        if(commodity.getStocks() < amount){
            throw new BaseException(RetCode.COMMODITY_STOCKS_INSUFFICIENT);
        }
        //检验该用户购物车是否存在该商品
        CartItem item = cartDao.selectCartItemByUserIdAndCommodityId(userId, commodityId);
        if(item != null){
            return this.modifyCommodityNum(item.getCartItemId(), item.getCommodityNum() + amount);
        }
        CartItem cartItem = new CartItem(userId, commodityId, storeId, amount);
        cartItem.setCartItemId(snowflake.nextId().toString());
        if(cartDao.insertCartItem(cartItem).equals(1)){
            return RetCode.CART_SUCCESS;
        }
        return RetCode.CART_INSERT_FAILURE;
    }

    @Override
    public RetCode deleteCart(String cartItemId) {
        if(cartDao.deleteCartItemByCartItemId(cartItemId).equals(0)){
            return RetCode.CART_DELETE_FAILURE;
        }
        return RetCode.CART_SUCCESS;
    }

    @Override
    public RetCode deleteCartList(List<String> cartItemIdList) {
        if(cartDao.deleteCartItemsByCartItemIdList(cartItemIdList).equals(0)){
            return RetCode.CART_DELETE_FAILURE;
        }
        return RetCode.CART_SUCCESS;
    }

    @Override
    public RetCode modifyCommodityNum(String cartItemId, Integer amount) {
        if(amount <= 0){
            throw new BaseException(RetCode.CART_COMMODITY_NUM_NONPOS);
        }
        CartItem cartItem = cartDao.selectCartItemByCartItemId(cartItemId);
        if(cartItem == null){
            throw new BaseException(RetCode.CART_ITEM_NOT_FOUND);
        }
        if(cartItem.getCommodity().getStocks() < amount){
            throw new BaseException(RetCode.COMMODITY_STOCKS_INSUFFICIENT);
        }
        if(cartDao.updateCommodityNumByCartItemId(cartItemId, amount).equals(0)){
            return RetCode.CART_MODIFY_FAILURE;
        }
        return RetCode.CART_SUCCESS;
    }
}
